﻿Imports Co9320.NamaAlert.ViewModel
Imports System.Xml.Serialization
Imports System.Security
Imports Xceed.Wpf.DataGrid.Settings

Class Application

    ' Startup、Exit、DispatcherUnhandledException などのアプリケーション レベルのイベントは、
    ' このファイルで処理できます。

    Private WithEvents ViewModel As MainWindowViewModel
    Private WithEvents Window As MainWindow

    Private Shared Path As New ApplicationPath

    Shared Sub New()

        'LocalDatabaseController.CreateDatabase("namaalert2.sdf", True)
        Initialize()

    End Sub

    Private Shared Sub Initialize()

        Path.UserDataFolder = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Co9320\NamaAlert\1.0.0.0")
        Path.CacheFolder = System.IO.Path.Combine(Path.UserDataFolder, "cache")
        Path.DatabaseFileName = System.IO.Path.Combine(Path.UserDataFolder, "namaalert.sdf")

        Try

            My.Computer.FileSystem.CreateDirectory(Path.UserDataFolder)
            My.Computer.FileSystem.CreateDirectory(Path.CacheFolder)

            Dim applicationPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)
            If Not System.IO.File.Exists(Path.DatabaseFileName) Then
                My.Computer.FileSystem.CopyFile(System.IO.Path.Combine(applicationPath, "namaalert.sdf"), Path.DatabaseFileName)
            End If

        Catch argEx As ArgumentException

        Catch ioEx As System.IO.IOException

        Catch unauth As UnauthorizedAccessException

        Catch securityEx As SecurityException

        End Try

    End Sub

    Protected Overrides Sub OnStartup(ByVal e As StartupEventArgs)
        MyBase.OnStartup(e)

        ' このソースコードを流用する場合は、ライセンスを取得（無料）してあなたのLicense Keyを設定してください。
        Xceed.Wpf.DataGrid.Licenser.LicenseKey = "DGF31-X8X57-07B1P-C85A"



        ViewModel = New MainWindowViewModel(Path)
        Dim list = New List(Of String)
        list.Add("a")
        Resources("ContextMenuDataContext") = ViewModel.LivePrograms

        AddHandler ViewModel.UISettings.DataGrid.RequestSetGroup, AddressOf SetGroup

        Window = New MainWindow
        Window.DataContext = ViewModel



        Window.Show()
    End Sub

    Private Sub Window_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Window.Closing

        '    Dim settings As New SettingsRepository()
        '    Window.LiveProgramDataGrid.SaveUserSettings(settings, UserSettings.All)

        '    Dim serializer As New XmlSerializer(GetType(SettingsRepository))

        '    Using stream As New FileStream("settings.xml", FileMode.CreateNew)
        '        serializer.Serialize(stream, settings)
        '    End Using
    End Sub

    Private Sub SetGroup(ByVal sender As Object, ByVal e As GroupEventArgs)
        Window.SetGroup(e.Group)
    End Sub

    Private Sub ViewModel_ApplicationShow(ByVal sender As Object, ByVal e As System.EventArgs) Handles ViewModel.ApplicationShow
        Window.Show()
    End Sub

    Private Sub ViewModel_ApplicationShutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles ViewModel.ApplicationShutdown
        Window.Shutdown()
    End Sub

    Private Sub ViewModel_RequestShowTopmost(ByVal sender As Object, ByVal e As BooleanEventArgs) Handles ViewModel.RequestShowTopmost
        Window.Topmost = e.State
    End Sub

End Class

